package com.shujia.day14;

import java.io.*;

public class Code07CopyFile {
    public static void main(String[] args) throws IOException {
        // 通过四种方式比较其执行效率
        //   copy1(); 大于4分钟

        // 通过字节数组形式
//        copy2(); //  1063毫秒

        // 通过缓冲字节流 读取单个字节
//        copy3(); // 8秒

        // 通过缓存字节流读取字节数组
        copy4();  // 800毫秒

    }


    public static void copy1() throws IOException {
        // 通过FileInputStream和FileOutputStream 单个字节进行循环复制
        FileInputStream fileInputStream = new FileInputStream("F:\\26qi\\file\\aa.mp4");
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\26qi\\file\\newfile\\bb.mp4");
        System.out.println("开始复制..");
        long start = System.currentTimeMillis();
        int read = 0;
        while ((read = fileInputStream.read()) != -1) {
            fileOutputStream.write(read);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完成,共耗时：" + (end - start) / 1000 + "秒");
        fileInputStream.close();
        fileOutputStream.close();
    }

    public static void copy2() throws IOException {
        // 通过FileInputStream和FileOutputStream 使用字节数组形式进行循环复制
        FileInputStream fileInputStream = new FileInputStream("F:\\26qi\\file\\aa.mp4");
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\26qi\\file\\newfile\\cc.mp4");
        System.out.println("开始复制..");
        long start = System.currentTimeMillis();

        byte[] bytes = new byte[1024];
        int read = 0;
        while ((read = fileInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完成,共耗时：" + (end - start) + "毫秒");
        fileInputStream.close();
        fileOutputStream.close();
    }

    public static void copy3() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("F:\\26qi\\file\\aa.mp4");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

        FileOutputStream fileOutputStream = new FileOutputStream("F:\\26qi\\file\\newfile\\dd.mp4");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);


        System.out.println("开始复制..");
        long start = System.currentTimeMillis();
        int read = 0;
        while ((read = bufferedInputStream.read()) != -1) {
            bufferedOutputStream.write(read);
        }
        bufferedOutputStream.flush(); // 注意 写出数据需要刷新
        long end = System.currentTimeMillis();
        System.out.println("复制完成,共耗时：" + (end - start) / 1000 + "秒");

        fileInputStream.close();
        bufferedInputStream.close();

        fileOutputStream.close();
        bufferedOutputStream.close();
    }

    public static void copy4() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("F:\\26qi\\file\\aa.mp4");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

        FileOutputStream fileOutputStream = new FileOutputStream("F:\\26qi\\file\\newfile\\ee.mp4");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);


        System.out.println("开始复制..");
        long start = System.currentTimeMillis();
        int read = 0;
        byte[] bytes = new byte[1024];
        while( (read = bufferedInputStream.read(bytes)) != -1){
            bufferedOutputStream.write(bytes);
            bufferedOutputStream.flush();
        }

        long end = System.currentTimeMillis();
        System.out.println("复制完成,共耗时：" + (end - start)  + "毫秒");

        fileInputStream.close();
        bufferedInputStream.close();

        fileOutputStream.close();
        bufferedOutputStream.close();
    }
}
